package view;

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.CheckBoxTableCell;
import javafx.scene.control.cell.ComboBoxTableCell;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.control.cell.TextFieldTableCell;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.util.StringConverter;

// 表格
public class TableViewDemo extends Application {
	private VBox root = new VBox(20);
	private TableView<Product> tv = new TableView<>();		
	private ObservableList<Product> products = FXCollections.observableArrayList();		// 可观察列表
	private HBox hBox = new HBox(10);
	private TextField nameInput;	
	private TextField priceInput;
	private TextField amountInput;
	private Button addButton;
	private Button deleteButton;
	
	public static void main(String[] args) {
		launch(args);
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public void start(Stage stage) {
		tv.setTableMenuButtonVisible(true);		// 增加选择各列是否可见的按钮
		tv.setEditable(true);	// 可编辑
//		tv.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);	 	// 尺寸设置
		
		// 名字列
		TableColumn<Product, String> nameColumn = new TableColumn<>("Name");  // 列的名字
		nameColumn.setCellValueFactory(new PropertyValueFactory<>("name"));   // 设置该列放的是哪个属性
		nameColumn.setMinWidth(180);
		nameColumn.setSortable(false); 		// 能否排序
		nameColumn.setCellFactory(TextFieldTableCell.forTableColumn());	// 设置编辑方式
		
		// 价格列
		TableColumn<Product, Double> priceColumn = new TableColumn<>("Price");
		priceColumn.setMinWidth(120);
		priceColumn.setCellValueFactory(new PropertyValueFactory<>("price"));
		priceColumn.setCellFactory(TextFieldTableCell.forTableColumn(new StringConverter<Double>() {	// 非字符串时

			@Override
			public String toString(Double object) {
				return String.valueOf(object);
			}

			@Override
			public Double fromString(String string) {
				return Double.valueOf(string);		// 应异常处理
			}
		}));
		
		// 数量列
		TableColumn<Product, Integer> amountColumn = new TableColumn<>("Amount");
		amountColumn.setMinWidth(120);
		amountColumn.setCellValueFactory(new PropertyValueFactory<>("amount"));
		amountColumn.setCellFactory(ComboBoxTableCell.forTableColumn(new StringConverter<Integer>() {	// 组合框编辑

			@Override
			public String toString(Integer object) {
				return String.valueOf(object);
			}

			@Override
			public Integer fromString(String string) {
				return Integer.valueOf(string);
			}
			
		}, 100, 200));
		
		// 合并列
		TableColumn<Product, Object> infoColumn = new TableColumn<>("info");		
		infoColumn.getColumns().add(priceColumn);
		infoColumn.getColumns().add(amountColumn);
		
		// 新鲜列
		TableColumn<Product, Boolean> freshColumn = new TableColumn<>("Fresh");  
		freshColumn.setCellValueFactory(new PropertyValueFactory<>("fresh"));   
		freshColumn.setMinWidth(120);
		freshColumn.setCellFactory(CheckBoxTableCell.forTableColumn(new TableColumn<Product, Boolean>()) );	  // 打钩
		
		// 加入列
		tv.getColumns().addAll(nameColumn, infoColumn, freshColumn);  
		products.addAll(new Product("chicken", 20, 10, true),		// 加入内容
					    new Product("pork", 15, 20, false), 
					    new Product("fish", 10, 30, false));
		tv.setItems(products);     // 增加和删除内容时不用刷新,修改时要刷新
		
		// 增加删除面板
		hBox.setPadding(new Insets(10));
		
		// 信息输入文本框
		nameInput = new TextField();
		nameInput.setPromptText("name");	// 灰色提示文本
		
		priceInput = new TextField();
		priceInput.setPromptText("price");
		
		amountInput = new TextField();
		amountInput.setPromptText("amount");
		
		// 按钮
		addButton = new Button("add");
		deleteButton = new Button("delete");
		hBox.getChildren().addAll(nameInput, priceInput, amountInput, addButton, deleteButton);
		root.getChildren().addAll(tv, hBox);
		
		Scene scene = new Scene(root);
		scene.getStylesheets().add(getClass().getResource("tableview.css").toExternalForm());
		stage.setScene(scene);
		stage.show();
		
		// 事件
		addButton.setOnMouseClicked(e -> addProduct());
		deleteButton.setOnMouseClicked(e -> deleteProduct());
	}
	
	// 增加产品
	private void addProduct() {
		Product product = new Product();
		product.setName(nameInput.getText());
		product.setPrice(Double.parseDouble(priceInput.getText()));
		product.setAmount(Integer.parseInt(amountInput.getText()));
		product.setFresh(false);
		tv.getItems().add(product);		// 两种方法
//		products.add(product);
		
		// 清空
		nameInput.clear();
		priceInput.clear();
		amountInput.clear();
	}
	
	// 删除产品
	private void deleteProduct() {
		int index = tv.getSelectionModel().getSelectedIndex();	// 选择项的下标
		products.remove(index);
	}
}

